package util.content;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import replaceWords.WeiYuanChuang;

public class ContentSpider {

	public int getContent() {
		final int contentSpiderInterval = 3000;
		String titlePub = null;
		String contentPub = null;
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		Connection connGetSite = null;
		Statement stmtGetSite = null;
		java.sql.ResultSet rs = null;
		java.sql.ResultSet rsGetSite = null;
		//初始化伪原创
		WeiYuanChuang wyc = new WeiYuanChuang();
		wyc.setWordDBName(wordDBName);
		wyc.setWordHost(wordHost);
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://" + host
					+ "/" + dBName + "?user=root");
			connGetSite = conn;
			stmtGetSite = connGetSite.createStatement();
			rsGetSite = stmtGetSite.executeQuery("select * from tagurl where urlnotail = '" + sourceNet + "' and pub = 0 ");
			while (rsGetSite.next()) {
				Long btime = System.currentTimeMillis();
				SpiderContentTxt sct = new SpiderContentTxt();
				sct.setRepeatTimesContent(repeatTimesContent);
				sct.setIntervalContent(intervalContent);
				sct.SpiderPageContent(rsGetSite.getString("url"));
				String contentString = sct.getWebSiteBodyString();
				String title = sct.getTitle();				
				sct.clear();
				sct = null;
				if(contentString.equalsIgnoreCase("0"))
				{
					pstmt = conn.prepareStatement("insert into content (id,title,content,sourceNet,pageURL,titlePub,contentPub) values (?,?,?,?,?,?,?)");
					pstmt.setInt(1, 0);
					pstmt.setString(2, title);
					pstmt.setString(3, contentString);
					pstmt.setString(4, sourceNet);
					pstmt.setString(5, rsGetSite.getString("url"));
					pstmt.setString(6, titlePub);
					pstmt.setString(7, contentPub);
					pstmt.execute();
				}
				else{
				//contentString、title处理，拿出有效的content和title
				//contnetPub和title是有效的发布出去的最终内容，其中content还经过伪原创处理
				titlePub = title.replaceAll(titleTrimRexContent, "").trim();			
				Pattern contentPattern = Pattern.compile("(" + titlePub + ")(" + contentEnding + ")(" + contentEnding + ")");
				Matcher contentMatcher = contentPattern.matcher(contentString);
				contentPub = contentMatcher.group(2).replaceAll(author_date, "");
				//调用伪原创
				wyc.setContent(contentPub);
				wyc.wyc();
				contentPub = wyc.getWordOK();
				pstmt = conn.prepareStatement("insert into content (id,title,content,sourceNet,pageURL,titlePub,contentPub) values (?,?,?,?,?,?,?)");
				pstmt.setInt(1, 0);
				pstmt.setString(2, title);
				pstmt.setString(3, contentString);
				pstmt.setString(4, sourceNet);
				pstmt.setString(5, rsGetSite.getString("url"));
				pstmt.setString(6, titlePub);
				pstmt.setString(7, contentPub);
				pstmt.execute();
				}
				System.out.println("现在抓取到url表的第" + (scheduleContentId+1) + "个数据");
				Long etime = System.currentTimeMillis();
				//如果不到2.5秒就加一段延时
				if ((etime - btime) < contentSpiderInterval) {
					Thread.sleep(intervalContent - (etime - btime));
				}
			}

		}  catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("SQLError");
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			//无论出错与否都打出现在处理的wangzhi表的数据id，下一次可以从这个项开始
			System.out.println("下次从第" + scheduleContentId + "个数据开始");
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
				if (rsGetSite != null) {
					rsGetSite.close();
					rsGetSite = null;
				}
				if (stmtGetSite != null) {
					stmtGetSite.close();
					stmtGetSite = null;
				}
				if (connGetSite != null) {
					connGetSite.close();
					connGetSite = null;
				}
				if (pstmt != null) {
					pstmt.close();
					pstmt = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 8;
	}
	String contentEnding = null;
	String titleTrimRexContent = null;
	String host = null;
	String dBName = null;
	String author_date = null;
	String sourceNet = null;
	String wordDBName = null;
	String wordHost = null;
	int scheduleContentId = 0;
	int repeatTimesContent = 0;
	int intervalContent = 0;
	
	public void setWordDBName(String wordDBName) {
		this.wordDBName = wordDBName;
	}
	
	public void setWordhost(String wordHost) {
		this.wordHost = wordHost;
	}

	public void setIntervalContent(int intervalContent) {
		this.intervalContent = intervalContent;
	}

	public void setRepeatTimesContent(int repeatTimesContent) {
		this.repeatTimesContent = repeatTimesContent;
	}
	
	public void setSourceNet(String sourceNet) {
		this.sourceNet = sourceNet;
	}

	public void setAuthor_date(String author_date) {
		this.author_date = author_date;
	}

	public void setHost(String host) {
		this.host = host;
	}
	public void setdBName(String dBName) {
		this.dBName = dBName;
	}

	public void setContentEnding(String contentEnding) {
		this.contentEnding = contentEnding;
	}

	public void setTitleTrimRexContent(String titleTrimRexContent) {
		this.titleTrimRexContent = titleTrimRexContent;
	}

}
